<?php
/**
 * @todo 获取拼音首字母
 * @param unknown_type $zh
 * @return Ambigous <string, NULL>
 */
function pinyin($zh){
	$ret = "";
	$s1 = @iconv("UTF-8","gb2312", $zh);
	$s2 = @iconv("gb2312","UTF-8", $s1);
	if($s2 == $zh){$zh = $s1;}
	for($i = 0; $i < strlen($zh); $i++){
		$s1 = substr($zh,$i,1);
		$p = ord($s1);
		if($p > 160){
			$s2 = substr($zh,$i++,2);
			$ret .= getfirstchar($s2);
		}else{
			$ret .= $s1;
		}
	}
	return $ret;
}
/**
 * 计算下一个月
 * @param type $time
 * @return type
 */
function next_month($n){
	$d = 32 * $n;	
	$time = time();
	$last_month_time = mktime(date("G", $time), date("i", $time),
			date("s", $time), date("n", $time), $d, date("Y", $time));
	$last_month_t =  date("t", $last_month_time);
	return date("Y-m", $last_month_time);
	//return date(date("Y-m", $last_month_time) . "-d", $time);
}
function getfirstchar($s0){
	$fchar = ord($s0{0});
	if($fchar >= ord("A") and $fchar <= ord("z") )return strtoupper($s0{0});
	$s1 = @iconv("UTF-8","gb2312", $s0);
	$s2 = @iconv("gb2312","UTF-8", $s1);
	if($s2 == $s0){$s = $s1;}else{$s = $s0;}
	$asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
	if($asc >= -20319 and $asc <= -20284) return "A";
	if($asc >= -20283 and $asc <= -19776) return "B";
	if($asc >= -19775 and $asc <= -19219) return "C";
	if($asc >= -19218 and $asc <= -18711) return "D";
	if($asc >= -18710 and $asc <= -18527) return "E";
	if($asc >= -18526 and $asc <= -18240) return "F";
	if($asc >= -18239 and $asc <= -17923) return "G";
	if($asc >= -17922 and $asc <= -17418) return "I";
	if($asc >= -17417 and $asc <= -16475) return "J";
	if($asc >= -16474 and $asc <= -16213) return "K";
	if($asc >= -16212 and $asc <= -15641) return "L";
	if($asc >= -15640 and $asc <= -15166) return "M";
	if($asc >= -15165 and $asc <= -14923) return "N";
	if($asc >= -14922 and $asc <= -14915) return "O";
	if($asc >= -14914 and $asc <= -14631) return "P";
	if($asc >= -14630 and $asc <= -14150) return "Q";
	if($asc >= -14149 and $asc <= -14091) return "R";
	if($asc >= -14090 and $asc <= -13319) return "S";
	if($asc >= -13318 and $asc <= -12839) return "T";
	if($asc >= -12838 and $asc <= -12557) return "W";
	if($asc >= -12556 and $asc <= -11848) return "X";
	if($asc >= -11847 and $asc <= -11056) return "Y";
	if($asc >= -11055 and $asc <= -10247) return "Z";
	return null;
}
/**
 * @todo 随机获取定长的字符串
 * @param unknown_type $len
 * @return string
 */
function mcRandStr($len=10){
	$arr = array('1','2','3','4','5','6','7','8','9','0','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
	$str = "";
	if(is_numeric($len) && $len > 0){
		for($i = 0; $i < $len; $i++){
			$n = rand(0, 35);
			$str .= $arr[$n];
		}
	}
	return $str;
}
/**
 * 登录--记住我
 * @param unknown_type $adminId
 */
function rememberMe($adminId,$salt){
	//setcookie("shop_cookie_01",$adminId,60480);//一周
	$time = time();
	$cookie = array(
			'name'   => 'shop_cookie_01',
			'value'  => $adminId . "_" . $time . "_" . md5($adminId.$time.$salt),
			'expire' => '604800'			
	);
	$ci = & get_instance();
	$ci->input->set_cookie($cookie);	
}
/**
 * 验证用户登录情况
 */
function checkLogin(){		
	if(!isset($_SESSION['isadmin']) || !$_SESSION['isadmin']){		
		//验证cookie	
		$ci = & get_instance();
		$loginCookie = explode("_", $ci->input->cookie("shop_cookie_01"));		
		$adminId = isset($loginCookie[0])?$loginCookie[0]:null;
		$time    = isset($loginCookie[1])?$loginCookie[1]:null;
		$hash    = isset($loginCookie[2])?$loginCookie[2]:null;
		if($adminId){
			$ci->load->model("AdminModel");
			$row = $ci->AdminModel->getRow("select id,username,salt from {$ci->AdminModel->getTable()} where id='$adminId'");
			if($row && md5($adminId.$time.$row['salt']) == $hash && $time + 604800 > time()){
				setLoginSession($row['id'],$row['username']);
			}
		}
	}
	if(!isset($_SESSION['isadmin']) || !$_SESSION['isadmin']){
		header("location:/index.php?c=login");
	}		
}
/**
 * 设置登录session
 * @param unknown_type $adminId
 * @param unknown_type $adminName
 */
function setLoginSession($adminId,$adminName,$super=0){	
	//$data = array('isadmin'=>true,"adminid"=>$adminId,"adminname"=>$adminName);
	$_SESSION['isadmin'] = true;
	$_SESSION['super']   = $super;
	$_SESSION['adminid'] = $adminId;
	$_SESSION['adminname'] = $adminName;		
}

function unsetLoginSession(){
	unset($_SESSION['isadmin']);
	unset($_SESSION['super']);
	unset($_SESSION['adminid']);
	unset($_SESSION['adminname']);	
}

function unsetLoginCookie(){
	$ci = & get_instance();
	$ci->load->helper("cookie");
	delete_cookie("shop_cookie_01");	
}

//接口中登陆相关

function isUserLogin(){
	if(!isset($_SESSION['userid']) || !$_SESSION['userid']){		
		return false;
	}
	return true;
}
//用户登陆
function setUserLogin($userId,$userName){
	$_SESSION['userid'] = $userId;
	$_SESSION['username'] = $userName;
}
function getLoginUserId(){
	if(isset($_SESSION['userid']) && $_SESSION['userid'])
		return $_SESSION['userid'];
	return null;
}
function getLoginUserName(){
	if(isset($_SESSION['userid']) && $_SESSION['userid'])
		return $_SESSION['userid'];
	return null;
}
